<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">工站名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入工站名称"
                                               autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">工站编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" id="code" class="layui-input"
                                               type="text"
                                               placeholder="输入工站编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">是否启用：</label>
                                    <div class="layui-input-inline">
                                        <select name="isDeleted">
                                            <option value="">请选择是否启用</option>
                                            <option value="1">启用</option>
                                            <option value="0">停用</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input date-icon" type="text"
                                               placeholder="请选择时间" style="width: 200px" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" type="hidden"/>
                                    <input name="endCreateTime" id="eTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="deviceWorkshopBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="workSiteTable" lay-filter="workSiteTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="workSiteTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="workSiteForm">
    <form lay-filter="workSiteForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">所在车间：</label>
            <div class="layui-input-block">
                <select name="workshopId" id="workshopId" class="layui-input"
                        type="text">
                </select>
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工站编号：</label>
            <div class="layui-input-block">
                <input name="code" placeholder="工位编号" type="text" class="layui-input" lay-verify="required"
                       required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工站名称：</label>
            <div class="layui-input-block">
                <input name="name" placeholder="工位名称" type="text" class="layui-input" lay-verify="required"
                       required autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">责任部门：</label>
            <div class="layui-input-block">
                <input id="departmentId" name="departmentId" type="hidden"/>
                <input id="departmentName" name="departmentName" type="hidden"/>
                <div id="departmentSearchClaim"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">责任岗位：</label>
            <div class="layui-input-block">
                <select id='postId' name="postId"
                        lay-filter="postId"
                        class="common-select-search">
                </select>
                <input id="postName" name="postName" type="hidden"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="workSiteFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var laydate = layui.laydate;

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    var dateValue = value.split(" - ");
                    $("#sTime").val(dateValue[0] + " 00:00:00");
                    $("#eTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#sTime").val();
                    $("#eTime").val();
                }
            }
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#workSiteTable',
            url: config.base + 'enterprise/res/admin/setting/workSite/list',
            page: true,
            toolbar: true,
            defaultToolbar: ["filter"],
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'}
                , {field: 'code', sort: false, title: '工位编号'}
                , {field: 'name', sort: false, title: '工位名称'}
                , {field: 'workshopName', sort: false, title: '所在车间'}
                , {field: 'postName', sort: false, title: '责任岗位'}
                , {
                    field: 'isDeleted', sort: false, title: '是否启用', templet: function (d) {
                        if (d.isDeleted) {
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="' + d.id + '" checked="' + d.isDeleted + '" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                        } else {
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="' + d.id + '" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                        }

                    }
                }
                , {field: 'creatorName', sort: false, title: '创建人'}
                , {field: 'createTime', sort: false, title: '创建时间'}
                , {align: 'center', toolbar: '#workSiteTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") === -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("工站-变更") === -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") === -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        //启用/禁用
        form.on('switch(isDeleted)', function (obj) {
            if (obj.elem.checked) {
                enable(obj.value)
            } else {
                disable(obj.value)
            }
        })

        function enable(id) {
            admin.req('enterprise/res/admin/setting/workSite/cancellation/'+id, null, function (res) {
                if (res.code === 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('workSiteTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id) {
            admin.req('enterprise/res/admin/setting/workSite/recover/'+id, null, function (res) {
                if (res.code === 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('workSiteTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 工具条点击事件
        table.on('tool(workSiteTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        //监听排序
        table.on('sort(workSiteTable)', function (obj) {
            table.reload('workSiteTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#deviceWorkshopBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '620px',
                offset: '65px',
                title: data ? '修改工位信息' : '添加工位信息',
                content: $('#workSiteForm').html(),
                success: function () {
                    if (data) {
                        form.val('workSiteForm', data);
                        //工位性质
                        //所属车间
                        admin.initDataSelect("enterprise/device/admin/deviceWorkshop/list?page=1&limit=999", null, "workshopId", data.workshopId, "车间", "name", "id");

                        //岗位
                        admin.initDataSelect("user/admin/role/getPostData", {roleId: data.departmentId}, "postId", data.postId, "岗位", null, null);

                        admin.initDataSelectTree(true, 'user/admin/role/getTreeData', null, 'departmentSearchClaim', data.departmentId, "部门", "departmentId", function (data) {// 获取部门ID
                            let departmentId = data.change[0].value;
                            let departmentName = data.change[0].name;
                            //赋值部门id
                            $('#departmentId').val(departmentId);
                            $('#departmentName').val(departmentName);
                            $('#postId').empty();
                            $('#postName').val("");
                            //部门下的岗位列表
                            admin.initDataSelect("user/admin/role/getPostData", {roleId: departmentId}, "postId", null, "岗位", null, null);
                        });
                    } else {
                        //部门列表
                        admin.initDataSelectTree(true, 'user/admin/role/getTreeData', null, 'departmentSearchClaim', null, "部门", "departmentId", function (data) {
                            // 获取部门ID
                            let departmentId = data.change[0].value;
                            let departmentName = data.change[0].name;
                            //赋值部门id
                            $('#departmentId').val(departmentId);
                            $('#departmentName').val(departmentName);
                            $('#postId').empty();
                            $('#postName').val("");
                            //部门下的岗位列表
                            admin.initDataSelect("user/admin/role/getPostData", {roleId: departmentId}, "postId", null, "岗位", null, null);
                        });

                        //所属车间
                        admin.initDataSelect("enterprise/device/admin/deviceWorkshop/list?page=1&limit=999", null, "workshopId", null, "车间", "name", "id");
                    }

                    // 监听 select 的值改变
                    form.on('select(postId)', function (data) {
                        let selectedText = data.elem[data.elem.selectedIndex].text;  // 获取选中的文本
                        $('#postName').val(selectedText);  // 打印显示的选项文本
                    });

                    // 表单提交事件
                    form.on('submit(workSiteFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/res/admin/setting/workSite/updateWorkSiteBy' : 'enterprise/res/admin/setting/workSite/addWorkSiteBy', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('workSiteTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST', true, 'application/json;charset=utf-8');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/device/admin/deviceWorkshop/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }
    });
</script>